package com.platform.config;

import com.platform.comm.config.IdGenProperties;
import com.platform.comm.repository.CustomBaseRepositoryImpl;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.platform.comm.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.*;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: JPA Repository configuration.
 *
 * @author libin created on 2020/10/17.
 */
@Configuration
@EnableConfigurationProperties(IdGenProperties.class)
@EnableJpaRepositories(basePackages = {"com.platform.**.repository"},
        repositoryBaseClass = CustomBaseRepositoryImpl.class)
@EnableTransactionManagement
@EnableSpringDataWebSupport
public class CommDaoConfig {

    @Bean
    @ConditionalOnProperty(prefix = "ncloud.cluster.id", name = {"workId", "centerId"})
    public SnowflakeIdWorker snowflakeIdWorker(IdGenProperties idGenProperties) {
        return new SnowflakeIdWorker(idGenProperties.getWorkId(), idGenProperties.getCenterId());
    }

    @Bean
    @Autowired
    public JPAQueryFactory queryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }

    @Bean
    @Primary
    public EntityManager entityManager(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
        return entityManagerFactory(builder, dataSource).getObject().createEntityManager();
    }

    @Bean
    @Primary
    public PlatformTransactionManager transactionManager(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
        return new JpaTransactionManager(entityManagerFactory(builder, dataSource).getObject());
    }

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
        Map<String, String> map = new HashMap<>(10);
        map.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        return builder.dataSource(dataSource).packages("com.platform.**.entities", "com.platform.**.entity").mappingResources().properties(map)
                .persistenceUnit("secondPersistenceUnit").build();
    }
}
